//
//  FindCollectionCell.m
//  Zaker
//
//  Created by dllo on 16/3/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FindCollectionCell.h"

@interface FindCollectionCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, assign) BOOL isUpLoading;//用来标记是上拉还是下拉

@end

@implementation FindCollectionCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        
        self.dataArr = [NSMutableArray array];
    }
    return self;
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = 0;
    [self.contentView addSubview:_tableView];
    [_tableView registerClass:[FindTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    [self handleData];
    
    //话题页下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleData)];
    [_tableView.mj_header beginRefreshing];
    
    //话题页上拉加载
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadnewdata)];
    if (_dataArr.count != 0) {
        [_tableView.mj_footer beginRefreshing];
    }
    self.number = 10;
}

//发现页上拉加载
- (void)loadnewdata
{
    NSString *str = @"http://dis.myzaker.com/api/list_discussion.php?_appid=iphone&_bsize=640_1136&_dev=iPhone%2C8.4.1&_idfa=B04A9F17-39FD-4ADF-9D92-9F93CE548EFE&_lat=38.882515&_lbs_city=%E5%A4%A7%E8%BF%9E&_lbs_province=%E8%BE%BD%E5%AE%81&_lng=121.539757&_mac=02%3A00%3A00%3A00%3A00%3A00&_net=wifi&_udid=ECFC153B-2246-4EB0-8D9F-510516697A0C&_uid=&_utoken=&_v=6.4.6&_version=6.45&act=more_discussion&except_recommend=Y";
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        NSArray *arr = resposeObject[@"data"][@"list"];
        NSArray *arrTemp = [NSArray array];
        arrTemp = [FindModel modelHandlerWithArray:arr];
        for (NSInteger i = 0; i < arrTemp.count; i++) {
            [self.dataArr addObject:arrTemp[i]];
        }
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    [_tableView.mj_header endRefreshing];
}

//发现页的数据处理
- (void)handleData
{
    NSString *str = @"http://dis.myzaker.com/api/list_discussion.php?_appid=iphone&_bsize=640_1136&_dev=iPhone%2C8.4.1&_idfa=B04A9F17-39FD-4ADF-9D92-9F93CE548EFE&_lat=38.882465&_lng=121.539864&_mac=02%3A00%3A00%3A00%3A00%3A00&_net=wifi&_udid=ECFC153B-2246-4EB0-8D9F-510516697A0C&_uid=&_utoken=&_v=6.4.5&_version=6.44&except_subscribe=N";
    self.dataArr = [NSMutableArray array];
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        NSArray *arr = resposeObject[@"data"][@"list"];
        self.dataArr =  [FindModel modelHandlerWithArray:arr];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    [_tableView.mj_header endRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //只有数组中有值的时候才执行赋值
    if (_dataArr.count != 0) {
        FindModel *m = _dataArr[indexPath.row];
        [cell.img sd_setImageWithURL:[NSURL URLWithString:m.pic]];
        cell.titlelable.text = m.title;
        cell.stitleLable.text = m.stitle;
    }
    cell.selectionStyle = 0;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindModel *model = _dataArr[indexPath.row];
    NSLog(@"%ld", indexPath.row);
    self.block(model.title, model.api_url);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
